/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.classdiagram.editpart;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.ElementImport;
import org.eclipse.uml2.uml.NamedElement;
import org.eclipse.uml2.uml.PrimitiveType;

import com.cea.papyrus.core.directedit.IDirectEdition;
import com.cea.papyrus.core.editor.ModelManager;
import com.cea.papyrus.core.editpart.GraphNodeWithSemanticEditPart;
import com.cea.papyrus.core.figures.IconFigure;
import com.cea.papyrus.core.figures.TaggedElementFigure;
import com.cea.papyrus.core.utils.Debug;
import com.cea.papyrus.diagraminterchange2.di2.GraphNode;
import com.cea.papyrus.diagraminterchange2.di2.Uml1SemanticModelBridge;

/**
 *  {@link PrimitiveType} element edit part.
 */
public class PrimitiveTypeEditPart extends GraphNodeWithSemanticEditPart {

//	/**
//	 * Manager for editing PrimitiveType name.
//	 */
//	private DirectEditManager manager;
//
//	/**
//	 * Manager of lightweight label controlers.
//	 */ 
//	private LabelControlerManager labelsMngr;

	/**
	 * Creates a new PrimitiveTypeEditPart.
	 * @param modelManager the model manager for this edit part
	 * @param graphNode the parent graph node
	 */
	public PrimitiveTypeEditPart(GraphNode graphNode, ModelManager modelManager) {
		super(graphNode,modelManager);
	}
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	protected IFigure createFigure() {
		// Force initialization of the manager when figure has changed
		directEditionMode = IDirectEdition.UNDEFINED_DIRECT_EDITOR;
		manager = null;
		
		// Try to get figure from ContainerWithSemanticEditPart
		IFigure fig = super.createFigure();
		// figure can be created by superclass => shape figure
		if (fig != null) {
			return fig;
		}
		// else
		return new TaggedElementFigure("primitiveType");		
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected void refreshVisuals() {
		Debug.debug(this, "refreshvisuals", channel);

		Dimension size = getGraphNode().getSize();
		Point p = getGraphNode().getPosition();
		Rectangle r = new Rectangle(p ,size);

		IFigure fig = getFigure();

		// if fig is a stereotype figure
		if (!(fig instanceof TaggedElementFigure)) {
			IconFigure icf= (IconFigure)getFigure();
			icf.setName(((NamedElement)getUmlElement()).getName());

		} else {
			// Retrieve current class and figure
			org.eclipse.uml2.uml.PrimitiveType currentPType = getUmlElement();
			com.cea.papyrus.umlutils.Element currentElement = new com.cea.papyrus.umlutils.Element(getUmlElement());

			TaggedElementFigure f = (TaggedElementFigure) fig; //get Class figure

			// if fig is a ClassFigure
			((TaggedElementFigure) fig).refreshColors(getGraphNode());
			((TaggedElementFigure) fig).refreshPresentation(getPresentation(), currentPType, getGraphNode());
			boolean hasIcon  = currentElement.hasIcon();
			boolean hasShape = currentElement.hasShape();
			//f.refreshStereotypes(currentElement.getStereotypes(), getPresentation(), hasIcon, hasShape);
			f.refreshStereotypes(f.stereotypesLabel(this), getPresentation(), hasIcon, hasShape);

			f.setName(currentPType.getName());// set the name of the class

			//qualifiedName
			GraphNode gp= (GraphNode)getGraphNode().getContainer();
			f.refreshQualifiedName(gp, getGraphNode(), getUmlElement());
		}
		((GraphicalEditPart) getParent()).setLayoutConstraint(this, fig, r);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public PrimitiveType getUmlElement() {
		Element e=((Uml1SemanticModelBridge)getGraphNode().getSemanticModel()).getElement();
		if(e instanceof ElementImport){//maybe this is a Element imports
			return (PrimitiveType)((ElementImport)e).getImportedElement();
		}
		return ((PrimitiveType)e);
	}

//	/**
//	 * Init the labels Manager and register label controlers.
//	 * This method should be called when a Direct Edit request is performed.
//	 */
//	protected void initLabelControlerManager() {
//		if( labelsMngr != null ) {
//			return;
//		}
//
//		// Create and init it.
//		labelsMngr = new LabelControlerManager();
//		labelsMngr.createLabelControler( getNameLabel(), 
//				new PropertyAccessor(){
//			public String getValue() { return getUmlElement().getName(); }
//			public void setValue(String value) {
//				getUmlElement().setName(value); 
//			}
//		});
//	}

	/**
	 * Returns the Name Label of this edit part.
	 * @return Label the label displaying the name of this primitive type
	 */
	protected Label getNameLabel() {
		IFigure fig = getFigure();
		if (fig instanceof IconFigure) {
			return (Label) fig;
		} else if (fig instanceof TaggedElementFigure) {
			return ((TaggedElementFigure) fig).getNameLabel();
		} else {
			Debug.error(this, "ClassEditPart::getTextFigure() : Unknown Figure type.", channel);
			return null;
		}
	}

//	/**
//	 * Init the label direct edit manager.
//	 * This method should be called when a Direct Edit request is performed.
//	 */
//	protected void initLabelEditManager() {
//		if(manager==null) {
//			// locator for the cell editor (on the name label)
//			CellEditorLocator locator = new LabelCellEditorLocator(getNameLabel());
//
//			// defining how properties are modified in the model
//			PropertyAccessor accessor = new PropertyAccessor() {
//				public String getPropertyName() {
//					return "Name";
//				}
//				public String getValue() {
//					return ((org.eclipse.uml2.uml.NamedElement) getUmlElement()).getName();
//				}
//				public void setValue(String value) {
//					((org.eclipse.uml2.uml.NamedElement) getUmlElement()).setName(value);  
//				}
//			};
//			// defines the controler: link between model and view
//			ILabelControler labelControler = new LabelControler(getNameLabel(), accessor);
//
//			// creates the editing manager.
//			manager = new LabelDirectEditManager(this, locator, labelControler);
//		}
//	}
//
//
//	/**
//	 * the direct edit mechanism requires the edit part to handle the direct edit
//	 * request.
//	 * 
//	 * @param req the request that called the direct edit.
//	 */
//	public void performDirectEdit(Request req) {
//		initLabelEditManager();
//		manager.show();
//	}
//
//	/* (non-Javadoc)
//	 * @see org.eclipse.gef.editparts.AbstractEditPart#performRequest(org.eclipse.gef.Request)
//	 */
//	/**
//	 * 
//	 * 
//	 * @param request 
//	 */
//	@Override
//	public void performRequest(Request request){
//		if (RequestConstants.REQ_DIRECT_EDIT.equals(request.getType())) {
//			performDirectEdit(request);
//		}
//	}
}
